package com.ycy;

import com.ycy.socket.constant.UrlRuleConstants;
import com.ycy.socket.HomePageSocketIo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author PanBingYu
 * @description
 * @date 2022-07-13 18:59
 */

@RestController
@RequestMapping(UrlRuleConstants.URL_PRE_FIX_FULL + "/alarm/v1")
public class AlarmController {

  private HomePageSocketIo homePageSocketIo;

  @Autowired
  public void setHomePageSocketIo(HomePageSocketIo homePageSocketIo) {
    this.homePageSocketIo = homePageSocketIo;
  }

  /**
   * 定时发送
   */
  @PostConstruct
  public void postConstruct() {
    new Thread(new Runnable() {
      @Override
      public void run() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        while (true) {
          try {
            Thread.sleep(5000);
          } catch (InterruptedException e) {
            e.printStackTrace();
          }
          receiveAlarmMsg("hi~ i`m server ," + simpleDateFormat.format(new Date()));
        }
      }
    }).start();

  }

  @PostMapping("/receiveAlarmMsg")
  public String receiveAlarmMsg(String msg) {
    homePageSocketIo.sendMessageByCorpCode(HomePageSocketIo.DEFAULT_CORP_CODE,
            HomePageSocketIo.EVENT_ALARM_OCCUR,
            msg);
    return "ok";
  }

}
